from django.shortcuts import render,redirect
from django.http import HttpResponse,JsonResponse
from django.core.urlresolvers import reverse

# Create your views here.
from .. import models
# 数据分页需导入
from django.core.paginator import Paginator
# 导入Q对象
from django.db.models import Q

# 显示订单
def orderindex(request):
    data = models.Order.objects.all()
    # -------------------搜索---------------------------
    types = request.GET.get('types')
    keywords = request.GET.get('keywords')
    print(types)
    print(keywords)
    if types:
        # 判断搜索的类型
        if types == "all":
            # Q 对象 <===> or   filter <====>  and
            data = data.filter(Q(shdz__contains = keywords)|Q(wl__contains = keywords)|Q(buytype__contains = keywords))
            print('aa')
        else:
            print('dasd')
    context = {'data':data}
    return render(request,'myadmin/order/index.html',context)

# 订单状态修改
def orderedit(request):
    id = request.GET.get('id')
    status = request.GET.get('status')
    ob = models.Order.objects.get(id=id)
    ob.status = status
    ob.save()
    return JsonResponse({'error':1,'msg':'修改成功'})



# 查看订单详情
def orderinfo(request):
    oid = request.GET.get('oid')
    data = models.Order.objects.get(id = oid)
    context = {'data':data}
    return render(request,'myadmin/order/info.html',context)